package net.zjitc.service.impl;

import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.Orderitem;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.OrderService;
import net.zjitc.vo.OrderConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/11 9:22
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProductMapper productMapper;



    @Override
    public List<Orders> findAll() {
        List<Orders> allOrder = orderMapper.findAllOrder();
        setOrderUser(allOrder);
        return allOrder;
    }

    private void setOrderUser(List<Orders> allOrder) {
        for (int i=0;i<allOrder.size();i++){
            Orders orders = allOrder.get(i);
            User user = userMapper.selectById(orders.getUser_id());
            orders.setUser(user);
        }

    }

    @Override
    public List<Orders> findByCondition(OrderConditionVo vo){
        List<Orders> ordersList = orderMapper.findByCondition(vo);
        setOrderUser(ordersList);
        return ordersList;
    }

    @Override
    public Orders findOrderDeatils(String id){
        Orders orders = orderMapper.findById(id);
        User user = userMapper.selectById(orders.getUser_id());
        orders.setUser(user);
        List<Orderitem> orderitemList = orderMapper.findByOid(orders.getId());
        for (Orderitem item:orderitemList){
            Product product = productMapper.findById(item.getPid());
            item.setProduct(product);
        }
        orders.setOrderitemList(orderitemList);
        return orders;
    }

    @Override
    public int deleteOrderById(String id) {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public int add(Orders orders) {
        return orderMapper.add(orders);

    }

    @Override
    public List<Orders> findOrderByUser(int id) {
        return orderMapper.findOrderByUser(id);
    }

    @Override
    public Order findById(String id) {
        return (Order) orderMapper.findById(id);
    }

    @Override
    public void update_state(String id) {
        orderMapper.update_state(id);
    }

    @Override
    public void delete(String id) {
        orderMapper.delete(id);
    }


}
